const config = require('../config/config');
const dataUrl = config.testServer;
const FormData = require('form-data')
const axios = require("axios");
const qs = require('qs');

module.exports = {
    getEventsPreview,
    getFourSafetyEvents,
    getFourMainChannelLeanEvents,
    getFourWorkingAreaLeanEvents
}

async function getEventsPreview(payload) {
    let url_device = dataUrl + "api/v1/common/getHomePageDevicesImpl"
    let data = qs.stringify({
        unionId: payload
    });

    let config = {
        method: 'post',
        maxBodyLength: Infinity,
        url: url_device,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: data
    };
    let result_device
    try {
        result_device = await axios.request(config)
    } catch (error) {
        return error
    }
    // console.log(result_device.data)
    let deviceNum = result_device.data.deviceInfos.length

    let url_safety_events = dataUrl + 'api/v1/data/getSafetyViolationByDuration'
    let safety_event_data = qs.stringify({
        unionId: payload,
        deviceId: "",
        startDate: "",
        endDate: "",
        riskLevel: "",
        algorithmType: "",
        eventType: "",
    })
    let config_safety = {
        method: 'post',
        maxBodyLength: Infinity,
        url: url_safety_events,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: safety_event_data
    };
    let result_safety_events
    try {
        result_safety_events = await axios.request(config_safety)
    } catch (error) {
        return error
    }
    let startSafetyEvent = result_safety_events.data.data[0]
    let url_lean_events = dataUrl + "api/v1/data/getLeanViolationByDuration"
    let lean_event_data = qs.stringify({
        unionId: payload,
        deviceId: "",
        startDate: "",
        endDate: "",
        algorithmType: "",
        eventType: "",
    })
    let config_lean = {
        method: 'post',
        maxBodyLength: Infinity,
        url: url_lean_events,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: lean_event_data
    };
    let result_lean_events
    try {
        result_lean_events = await axios.request(config_lean)
    } catch (error) {
        return error
    }
    let startLeanEvent = result_lean_events.data.data[0]
    let continueDaySafety = Math.floor((new Date().getTime() - new Date(startSafetyEvent.startTime).getTime()) / (1000 * 60 * 60 * 24))
    let continueDayLean = Math.floor((new Date().getTime() - new Date(startLeanEvent.time).getTime()) / (1000 * 60 * 60 * 24))
    let eventsNum = result_lean_events.data.data.length + result_safety_events.data.data.length
    let continueDay = continueDaySafety > continueDayLean ? continueDaySafety : continueDayLean

    return {
        locationNum: deviceNum,
        eventsNum: eventsNum,
        continueDay: continueDay
    }


}

async function getFourSafetyEvents(payload) {
    let url_safety_events = dataUrl + 'api/v1/data/getSafetyViolationByDuration'
    let safety_event_data = qs.stringify({
        unionId: payload,
        deviceId: "",
        startDate: "",
        endDate: "",
        riskLevel: "",
        algorithmType: "",
        eventType: "",
    })
    let config_safety = {
        method: 'post',
        maxBodyLength: Infinity,
        url: url_safety_events,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: safety_event_data
    };
    let result_safety_events
    try {
        result_safety_events = await axios.request(config_safety)
    } catch (error) {
        return error
    }
    let latestData = result_safety_events.data.data.slice(-4)
    let freshLatestData = []
    for (let i = 0; i < latestData.length; i++) {
        let deviceInfo
        switch (latestData[i].deviceId) {
            case 2:
                deviceInfo = '1号门西1'
                break;
            case 4:
                deviceInfo = '1号塔吊'
                break;
            case 5:
                deviceInfo = '1号闸机'
                break;
            case 6:
                deviceInfo = '北围墙1'
                break;
            case 7:
                deviceInfo = '北围墙2'
                break;
            case 8:
                deviceInfo = '北围墙3'
                break;
            case 9:
                deviceInfo = '北围墙4'
                break;
            case 11:
                deviceInfo = '地磅房'
                break;
            case 12:
                deviceInfo = '施工道路'
                break;
            case 13:
                deviceInfo = '施工现场球机'
                break;
            case 14:
                deviceInfo = '围挡1'
                break;
            case 15:
                deviceInfo = '洗车池'
                break;

            default:
                deviceInfo = '未知'
                break;
        }

        let eventType
        switch (latestData[i].eventType) {
            case "safety_equipment_with_area":
                eventType = "识别区内未佩戴安全帽"
                break;
            case "safety_equipment_without_area":
                eventType = "识别区外未佩戴安全帽"
                break;
            case "safety_area_with_area":
                eventType = "闯入危险区"
                break;
            case "safety_area_without_area":
                eventType = "未佩戴安全帽闯入危险区域"
                break;
            case "fire_smoke_alarm":
                eventType = "火灾隐患事件"
                break;
            default:
                eventType = "未知"
                break
        }
        let obj = {
            deviceInfo: deviceInfo,
            eventType: eventType,
            videoUrl: result_safety_events.data.statisticUrl + latestData[i].url,
            startTime: latestData[i].startTime
        }
        freshLatestData.push(obj)
    }
    return {
        data: freshLatestData
    }
}

async function getFourMainChannelLeanEvents(payload) {
    let url_lean_events = dataUrl + "api/v1/data/getLeanViolationByDuration"
    let lean_event_data = qs.stringify({
        unionId: payload,
        deviceId: "",
        startDate: "",
        endDate: "",
        algorithmType: "0",
        deviceType: "",
    })
    let config_lean = {
        method: 'post',
        maxBodyLength: Infinity,
        url: url_lean_events,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: lean_event_data
    };
    let result_lean_events
    try {
        result_lean_events = await axios.request(config_lean)
    } catch (error) {
        return error
    }
    // console.log(result_lean_events.data)
    let latestData = result_lean_events.data.data.slice(-4)
    let freshLatestData = []
    for (let i = 0; i < latestData.length; i++) {
        let deviceInfo
        switch (latestData[i].deviceId) {
            case 1:
                deviceInfo = '1号大门北1'
                break;
            case 2:
                deviceInfo = '1号门西1'
                break;
            case 3:
                deviceInfo = '1号门西2'
                break;
            case 4:
                deviceInfo = '1号塔吊'
                break;
            case 6:
                deviceInfo = '北围墙1'
                break;
            case 7:
                deviceInfo = '北围墙2'
                break;
            case 8:
                deviceInfo = '北围墙3'
                break;
            case 9:
                deviceInfo = '北围墙4'
                break;
            case 11:
                deviceInfo = '地磅房'
                break;
            case 12:
                deviceInfo = '施工道路'
                break;
            case 13:
                deviceInfo = '施工现场球机'
                break;
            case 14:
                deviceInfo = '围挡1'
                break;

            default:
                deviceInfo = '未知'
                break;
        }
        let eventType = "主通道车辆占道"
        let obj = {
            deviceInfo: deviceInfo,
            eventType: eventType,
            time: latestData[i].time,
            imgUrl: result_lean_events.data.statisticUrl + latestData[i].imgUrl
        }
        freshLatestData.push(obj)
    }
    return {
        data: freshLatestData
    }
}

async function getFourWorkingAreaLeanEvents(payload) {
    let url_lean_events = dataUrl + "api/v1/data/getLeanViolationByDuration"
    let lean_event_data = qs.stringify({
        unionId: payload,
        deviceId: "",
        startDate: "",
        endDate: "",
        algorithmType: "1",
        deviceType: "",
    })
    let config_lean = {
        method: 'post',
        maxBodyLength: Infinity,
        url: url_lean_events,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: lean_event_data
    };
    let result_lean_events
    try {
        result_lean_events = await axios.request(config_lean)
    } catch (error) {
        return error
    }
    let latestData = result_lean_events.data.data.slice(-4)
    let freshLatestData = []
    for (let i = 0; i < latestData.length; i++) {
        let deviceInfo
        console.log(latestData[i].deviceType)
        switch (latestData[i].deviceId) {
            case 1:
                deviceInfo = '1号大门北1'
                break;
            case 2:
                deviceInfo = '1号门西1'
                break;
            case 3:
                deviceInfo = '1号门西2'
                break;
            case 4:
                deviceInfo = '1号塔吊'
                break;
            case 6:
                deviceInfo = '北围墙1'
                break;
            case 7:
                deviceInfo = '北围墙2'
                break;
            case 8:
                deviceInfo = '北围墙3'
                break;
            case 9:
                deviceInfo = '北围墙4'
                break;
            case 11:
                deviceInfo = '地磅房'
                break;
            case 12:
                deviceInfo = '施工道路'
                break;
            case 13:
                deviceInfo = '施工现场球机'
                break;
            case 14:
                deviceInfo = '围挡1'
                break;

            default:
                deviceInfo = '未知'
                break;
        }
        let eventType = "作业面建材未清理"
        let obj = {
            deviceInfo: deviceInfo,
            eventType: eventType,
            time: latestData[i].time,
            imgUrl: result_lean_events.data.statisticUrl + latestData[i].imgUrl
        }
        freshLatestData.push(obj)
    }
    return {
        data: freshLatestData
    }
}

async function getTowerCraneData(payload) {

}